/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.SavingBookManager;

import BLL.Deposit_BLL;
import BLL.Unity;
import DAL.ConnectUnit;
import DTO.Deposit_DTO;
import java.awt.Frame;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import static javax.swing.WindowConstants.HIDE_ON_CLOSE;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Nguyen Phi Hung
 */
public class JF_Deposit extends javax.swing.JFrame {
    Unity unity = new Unity();
    Deposit_BLL deposit_bll = new Deposit_BLL();
    SavingBookManager savingbook;
    /**
     * Creates new form JF_Deposit
     */
    public JF_Deposit() throws Exception {
        initComponents();
        savingbook = new SavingBookManager();
        MaSo_Deposit.setText(SavingBookManager.txtMaSo);
        MaSo_Deposit.setEditable(false);
        NgayGui_Deposit.setDate(new Date());
        CMND_Deposit.setDocument(getPlainDocument(11));
        XuatPhieu.setEnabled(false);
        setLocation(450,250);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel3 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        MaSo_Deposit = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        HoTen_Deposit = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        CMND_Deposit = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        SoTienGui_Deposit = new javax.swing.JTextField();
        NgayGui_Deposit = new org.jdesktop.swingx.JXDatePicker();
        GuiTien = new javax.swing.JButton();
        XuatPhieu = new javax.swing.JButton();
        NhapLai = new javax.swing.JButton();

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${defaultCloseOperation}"), this, org.jdesktop.beansbinding.BeanProperty.create("defaultCloseOperation"));
        bindingGroup.addBinding(binding);

        jLabel9.setText("Mã Sổ");

        jLabel10.setText("Họ Tên");

        HoTen_Deposit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                HoTen_DepositKeyTyped(evt);
            }
        });

        jLabel11.setText("CMND");

        CMND_Deposit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                CMND_DepositKeyTyped(evt);
            }
        });

        jLabel13.setText("Ngày Gửi");

        jLabel12.setText("Số Tiền Gửi");

        SoTienGui_Deposit.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                SoTienGui_DepositKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(MaSo_Deposit))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CMND_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(HoTen_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(72, 72, 72)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addGap(10, 10, 10))
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(SoTienGui_Deposit)
                    .addComponent(NgayGui_Deposit, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SoTienGui_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MaSo_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(HoTen_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NgayGui_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CMND_Deposit, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        GuiTien.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_Save.png"))); // NOI18N
        GuiTien.setText("Gửi Tiền");
        GuiTien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GuiTienActionPerformed(evt);
            }
        });

        XuatPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_TradeBill.png"))); // NOI18N
        XuatPhieu.setText("Xuất Phiếu");
        XuatPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                XuatPhieuActionPerformed(evt);
            }
        });

        NhapLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Small_Update.png"))); // NOI18N
        NhapLai.setText("Nhập Lại");
        NhapLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NhapLaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(GuiTien, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(XuatPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GuiTien, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(XuatPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NhapLai, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public static PlainDocument getPlainDocument(final int maxLength) {
        PlainDocument p = new PlainDocument() {
            @Override
            public void insertString(int arg0, String arg1, AttributeSet arg2)
                    throws BadLocationException {
// TODO Auto-generated method stub
                if (getLength() + arg1.length() <= maxLength) {
                    super.insertString(arg0, arg1, arg2);
                }
            }
        };
        return p;
    }
    
    private void HoTen_DepositKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_HoTen_DepositKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if ((c < 32) || (c > 32 && c < 65) || (90 < c && c < 97) || c > 122) {
            evt.consume();
        }
        /*
        if((Character.isDigit(c)||(c==KeyEvent.VK_DELETE)))
        {
            evt.consume();
        }*/
        ///
        // if((c<'0')||(c>'9'&&c<'A')||(c>'Z'&&c<'a')||(c>'z')||(c!=' '))
        //  evt.consume();
    }//GEN-LAST:event_HoTen_DepositKeyTyped

    private void CMND_DepositKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_CMND_DepositKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if ((c < 48 || c > 57)) {
            evt.consume();
        }
    }//GEN-LAST:event_CMND_DepositKeyTyped

    private void SoTienGui_DepositKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SoTienGui_DepositKeyTyped
        // TODO add your handling code here:
        char c = evt.getKeyChar();
        if ((c < 48 || c > 57)) {
            evt.consume();
        }
    }//GEN-LAST:event_SoTienGui_DepositKeyTyped

    private void GuiTienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GuiTienActionPerformed
        // TODO add your handling code here:
        Deposit_DTO info = new Deposit_DTO();
        info.setBookID(MaSo_Deposit.getText().trim());
        info.setCustomerName(HoTen_Deposit.getText().trim());
        info.setIDNumber(CMND_Deposit.getText().trim());
        info.setDepositingDate(unity.ConvertDate(NgayGui_Deposit));
        info.setAmountOfMoney(Float.parseFloat(SoTienGui_Deposit.getText().trim()));
        //kiem tra so tien gui

        try {
            boolean KiemTraSoTienGui = deposit_bll.KiemTraSoTienGui("{call KiemTra_SoTienGui(?,?)}", info.getAmountOfMoney());
            if (KiemTraSoTienGui) {
                deposit_bll.Insert("{call Insert_Deposit(?,?,?,?,?)}", info);
                JOptionPane.showMessageDialog(null, "Gửi Thành Công");
                XuatPhieu.setEnabled(true);
            } else {
                JOptionPane.showMessageDialog(null, "Số Tiền Gửi Phải Lớn Hơn Số Tiền Gửi Tối Thiểu");
            }

        } catch (Exception e) {
        }
    }//GEN-LAST:event_GuiTienActionPerformed

    private void XuatPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_XuatPhieuActionPerformed
        // TODO add your handling code here:
        try {
            new JF_Deposit().ShowReport();
        } catch (Exception ex) {
            Logger.getLogger(JF_Deposit.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_XuatPhieuActionPerformed

    private void ShowReport() throws JRException, ClassNotFoundException, SQLException, Exception{
        JasperReport report = JasperCompileManager.compileReport("src\\report\\report_deposit.jrxml");
        HashMap parameters = new HashMap();

        ConnectUnit connection = DAL.DAL.getDAL();
        Connection con = connection.connect.getConnect();
        JasperPrint print = JasperFillManager.fillReport(report, null, con);
        JRViewer viewer = new JRViewer(print);            

        viewer.setVisible(true);

        JasperViewer view = new JasperViewer(print);
        view.viewReport(print);
        view.setLocationRelativeTo(this);
        
        view.setExtendedState(Frame.MAXIMIZED_BOTH);
        view.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
    
    private void NhapLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NhapLaiActionPerformed
        // TODO add your handling code here:
        HoTen_Deposit.setText("");
        CMND_Deposit.setText("");
        SoTienGui_Deposit.setText("0.0");
    }//GEN-LAST:event_NhapLaiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JF_Deposit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JF_Deposit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JF_Deposit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JF_Deposit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new JF_Deposit().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(JF_Deposit.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField CMND_Deposit;
    private javax.swing.JButton GuiTien;
    private javax.swing.JTextField HoTen_Deposit;
    private javax.swing.JTextField MaSo_Deposit;
    private org.jdesktop.swingx.JXDatePicker NgayGui_Deposit;
    private javax.swing.JButton NhapLai;
    private javax.swing.JTextField SoTienGui_Deposit;
    private javax.swing.JButton XuatPhieu;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
